package com.gpnu.crm.web.controller;

import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageInfo;
import com.gpnu.crm.domain.Department;
import com.gpnu.crm.domain.Employee;
import com.gpnu.crm.query.EmployeeQueryObject;
import com.gpnu.crm.service.IDepartmentService;
import com.gpnu.crm.service.IEmployeeService;
import com.gpnu.crm.util.JSONResult;





@Controller
@RequestMapping("/employee")
public class EmployeeController {
	@Autowired
	private IEmployeeService employeeService;
	
	@Autowired
	private IDepartmentService departmentService;

	//访问到这个方法，localhost:8080/employee/list.do
	@RequestMapping("/list")
	public String list(@ModelAttribute("qo") EmployeeQueryObject qo, Model model) {
		//model相当于作用域request
		PageInfo<Employee> info = employeeService.list(qo);
		List<Department> departments = departmentService.list();
		//访问模型中
		model.addAttribute("pageInfo", info);
		model.addAttribute("depts", departments);
		return "employee/list"; //跳转到员工列表界面作数据展示
	}
	
	@RequestMapping("/delete")
	public String delete(Long id){
		employeeService.delete(id);
		return "redirect:/employee/list.do";
	}
	
	@RequestMapping("/input")
	public String input(Model model,Long id){
		//查询需要编辑的数据
		Employee e = employeeService.get(id);
		List<Department> departments = departmentService.list();
		model.addAttribute("e",e);
		model.addAttribute("depts", departments);
		return "employee/input";
	}
	
	
	//接受用户传递的参数（name/email/age），告知service将其保存到数据库中
	@RequestMapping("/saveOrUpdate")
	public String saveOrUpdate(Employee e){
		employeeService.saveOrUpdate(e);
		return "redirect:/employee/list.do";
	}

	@RequestMapping("/queryNewSellers")
	@ResponseBody
	public List<Employee> queryNewSellers(int id){
		List<Employee> list = employeeService.queryNewSellers(id);
		return list;
	}
	
	@RequestMapping({"batchDelete"})
	@ResponseBody
	public Object batchDelete(Long[] ids) { 
		this.employeeService.batchDelete(ids);
	    return new JSONResult(); 
	}
	
	@RequestMapping({"exportExcel"})
	public ModelAndView exportExcel(HttpServletResponse resp) throws Exception
	{
		List<Employee> list = this.employeeService.list();
	    Workbook wb = new HSSFWorkbook();
	    Sheet sheet = wb.createSheet();
	    Row row = sheet.createRow(0);
	    row.createCell(0).setCellValue("姓名");
	    row.createCell(1).setCellValue("邮箱");
	    row.createCell(2).setCellValue("年龄");
	    row.createCell(3).setCellValue("部门");
	    int index = 1;
	    for (Employee emp : list) {
	    	row = sheet.createRow(index++);
	    	row.createCell(0).setCellValue(emp.getName());
	    	row.createCell(1).setCellValue(emp.getEmail());
	    	row.createCell(2).setCellValue(emp.getAge().intValue());
	    	if (emp.getDept() != null) {
	    		row.createCell(3).setCellValue(emp.getDept().getName());
	    	}
	    }
	    resp.setHeader("Content-Disposition", "attachment;filename=employee.xls");
	    wb.write(resp.getOutputStream());
	    return null;
	  }
	  @RequestMapping({"importExcel"})
	  public String importExcel(MultipartFile xls) throws Exception {
		  
		  if ((xls != null) && (xls.getSize() > 0L) && (xls.getContentType().equals("application/vnd.ms-excel"))) {
		     Workbook wb = new HSSFWorkbook(xls.getInputStream());
		     Sheet sheet = wb.getSheetAt(0);
		     int lastRowNum = sheet.getLastRowNum();
		     Department d = new Department();
		     d.setId(Long.valueOf(1L));
		     for (int i = 1; i <= lastRowNum; i++){
		        Row row = sheet.getRow(i);
		        String name = row.getCell(0).getStringCellValue();
		        String email = row.getCell(1).getStringCellValue();
		        Double age = Double.valueOf(row.getCell(2).getNumericCellValue());
		        Employee emp = new Employee(name, "1", email, Integer.valueOf(age.intValue()), false, d, null);
		        this.employeeService.saveOrUpdate(emp);
		     }
		  }
		  return "redirect:/employee/list.do";
	  }

}
